package education.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.classic.Session;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import education.dao.TestTypeDao;
import education.exception.DAOException;
import education.model.Admin;
import education.model.Course;
import education.model.TestType;

@Component("testTypeDao")
public class TestTypeDaoImpl implements TestTypeDao {
	private HibernateTemplate hibernateTemplate;

	public boolean delete(int id) {
		Session session = hibernateTemplate.getSessionFactory().openSession();
		Query query = session
				.createQuery("delete TestType testType where testType.id=?");
		query.setInteger(0, id);
		int re = query.executeUpdate();
		session.close();
		if (re != 0) {
			return true;
		}
		return false;
	}

	public void delete(TestType testType) {
		hibernateTemplate.delete(testType);

	}

	public int getCount() {
		Session session = hibernateTemplate.getSessionFactory().openSession();
		Query query = session.createQuery("select count(*) from TestType");
		int counts = Integer.parseInt(query.list().get(0).toString());
		session.close();
		return counts;
	}

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	public TestType loadById(int id) {
		Session session = hibernateTemplate.getSessionFactory()
				.getCurrentSession();
		return (TestType) session.get(TestType.class, id);
	}

	public List<TestType> paging(int pageSize, int pageCount, int rowCount) {
		List<TestType> list;
		pageSize = (pageSize - 1) * rowCount;
		System.out.println("pageSize = " + pageSize);
		Session session = hibernateTemplate.getSessionFactory()
				.getCurrentSession();
		Query query = session.createQuery("from TestType").setFirstResult(
				pageSize).setMaxResults(rowCount);
		list = query.list();
		return list;
	}

	public void save(TestType testType) {
		hibernateTemplate.save(testType);
	}

	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public boolean update(TestType testType) {
		hibernateTemplate.update(testType);
		return true;
	}

	public List<TestType> getTestTypeList() {
		
		return hibernateTemplate.find("from TestType");
	}

	public List<Course> getCourseList() throws DAOException {
		// TODO Auto-generated method stub
		return null;
	}
}
